﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Text;
using System.Collections.Specialized;
using System.Collections;  

namespace JavaScriptTest._21_AJAX
{
    /// <summary>
    /// xmlhhtp 的摘要说明
    /// </summary>
    public class xmlhhtp : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["type"];

            string Data = getPostData(context);
            string ret = "";
            switch (type)
            {
                case "tianqi":
                    ret = getTianqi(); break;
                case "postData":
                    ret = postData(Data); break;
                default :
                    ret = ""; break;
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(ret);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        public string getTianqi()
        {
            return "今天天气很好";
        }

        public string postData( string Data) {
            return "接收到的数据是:" + Data;
        }


        // 获取POST返回来的数据  
        private string getPostData(HttpContext context)
        {
            try
            {
                System.IO.Stream s = context.Request.InputStream;
                int count = 0;
                byte[] buffer = new byte[1024];
                StringBuilder builder = new StringBuilder();
                while ((count = s.Read(buffer, 0, 1024)) > 0)
                {
                    builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
                }
                s.Flush();
                s.Close();
                s.Dispose();
                return builder.ToString();
            }
            catch (Exception ex)
            { throw ex; }
        } 
    }
}